<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/layouts/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>微信公众号</title>
	<%@include file="/WEB-INF/layouts/index-header.jsp"%>
</head>
<body>

<div class="tpanel">
	<div class="panel-content">
		<div class="container-fluid">
			<div id="tool">
				<form id="searchForm" class="form-inline" onsubmit="return search('#searchForm','#table')" style="margin-top:20px;margin-bottom:20px;">
					<div class="form-group">
						<label class="">公众号名称：</label>
						<input type="text" name="search_LIKES_name" class="form-control"/>
					</div>
					<div class="form-group">
						<button type="submit" class="btn btn-primary radius">
							<span class="glyphicon glyphicon-search"></span> 查询
						</button>
						<a class="btn btn-success radius" href="${ctx}/admin/wechat-mp/create">
							<span class="glyphicon glyphicon-cog"></span> 创建
						</a>
					</div>
				</form>
			</div>
			<table id="table"></table>
		</div>
	</div>
</div>

<script type="text/javascript">
    changeTitle('微信公众号');

    $(function() {
        $('#table').bootstrapTable({
            url: '${ctx}/admin/wechat-mp/page-list',
            idField : 'id',
            striped : true,
            pagination : true,
            pageNumber : 1,
            pageSize : 25,
            sidePagination : 'server',
            uniqueId : 'id',
            /*checkbox : true,
            clickToSelect : true,*/
            queryParams : queryParams,
            responseHandler : responseHandler,
            columns: [/*{
                field: 'ck',
                checkbox : true
            },*/
			{
				title: '#',
				width : 50,
				align : 'center',
				formatter: snFormatter
			},
			{
				field: 'name',
				title: '公众号名称',
				align : 'center',
				formatter : function(value,row,index) {
				    var html = '';
				    if(row.isCurrent) {
                    	html += '<span class="label label-success">当前</span> ';
					}
                    if(row.isDefault) {
                        html += '<span class="label label-primary">默认</span> ';
                    }
					return html + value;
				}
			},
			{
				field: 'appId',
				title: '公众号AppId',
				align : 'center'
			},
			{
                field: 'op',
                title: '操作',
                align : 'center',
                formatter : operateFormatter
            }]
        });

    });

    function operateFormatter(value,row,index) {
        var content = '';
        if(row.isCurrent) {
            content += '<i class="iconfont icon-zhuye"></i>';
		}else {
            content += '<a title="设为当前公众号" href="javascript:setCurrentMp(' + row.id + ')"><i class="iconfont icon-zhuye"></i></a>';
		}
        content += '&nbsp;&nbsp;&nbsp;&nbsp;';
        if(row.isDefault) {
            content += '<i class="iconfont icon-shezhi"></i>';
        }else {
            content += '<a title="设为默认公众号" href="javascript:setDefaultMp(\'' + row.mpId + '\')"><i class="iconfont icon-shezhi"></i></a>';
        }
        content += '&nbsp;&nbsp;&nbsp;&nbsp;';
		content += '<a title="查看详情" href="javascript:detail(' + row.id + ')"><i class="iconfont icon-caidanguanli"></i></a>';
        content += '&nbsp;&nbsp;&nbsp;&nbsp;';
        content += '<a href="${ctx}/admin/wechat-mp/update?id=' + row.id + '" title="编辑"><i class="iconfont icon-iconfontcolor32"></i></a>';
        content += '&nbsp;&nbsp;&nbsp;&nbsp;';
        content += '<a href="javascript:del(' + row.id + ')" title="删除"><i class="iconfont icon-shanchu"></i></a>';
        return content;
    }
    function detail(id) {
        openWindow('查看详情','${ctx}/admin/wechat-mp/detail?id=' + id);
    }
    function setCurrentMp(id) {
        window.parent.bootbox.prompt({
            title: '请输入您的管理密码以确定切换公众号',
            onEscape : true,
            backdrop : true,
            animate : true,
            buttons: {
                confirm: {
                    label: '确定',
                    className: 'btn-success'
                },
                cancel: {
                    label: '取消',
                    className: ''
                }
            },
            inputType: 'password',
            callback: function (result) {
                if(result) {
                    $.post("${ctx}/admin/wechat-mp/set-current-mp",{wechatMpId:id,password:result},function(resp) {
                        if(resp.code == 'OK') {
                            window.parent.layer.msg('切换成功', {icon: 1});
                            $('#table').bootstrapTable('refresh');
                        }else {
                            window.parent.layer.msg('切换失败:' + resp.message, {icon: 2});
                        }
                    },'json');
                }
            }
        });
	}
    function setDefaultMp(id) {
        window.parent.bootbox.prompt({
            title: '请输入您的管理密码以切换默认公众号',
            onEscape : true,
            backdrop : true,
            animate : true,
            buttons: {
                confirm: {
                    label: '确定',
                    className: 'btn-success'
                },
                cancel: {
                    label: '取消',
                    className: ''
                }
            },
            inputType: 'password',
            callback: function (result) {
                if(result) {
                    $.post("${ctx}/admin/wechat-mp/set-default-mp",{mpId:id,password:result},function(resp) {
                        if(resp.code == 'OK') {
                            window.parent.layer.msg('切换成功', {icon: 1});
                            $('#table').bootstrapTable('refresh');
                        }else {
                            window.parent.layer.msg('切换失败:' + resp.message, {icon: 2});
                        }
                    },'json');
                }
            }
        });
    }
    function del(id,name) {
        carefulDelete('${ctx}/admin/wechat-mp/careful-delete',id,'请输入您的管理密码以确定删除此系统资源');
    }
</script>

</body>
</html>
